<!--
 * Copyright © 2022 XXXXXXXXX
 * @Author: zhangxiang
 * @Date: 2022-08-04 18:01:36
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-09-21 15:02:02
 * @Description:
 *
-->

<template>
  <div class="namespace-mgmt-ctn">
    <el-row type="flex" style="margin-bottom: 20px">
      <el-input
        v-model="searchKey"
        style="width: 200px; margin-right: 20px"
        size="small"
        class="input"
        placeholder="请输入关键字进行过滤"
        clearable
        @clear="clearClick"
        @keyup.enter.native="queryClick"
      />
      <el-button
        size="small"
        type="primary"
        @click="queryClick"
      >查询</el-button>
      <el-button
        size="small"
        type="primary"
        style="margin-left: auto"
        icon="el-icon-plus"
        @click="addClick"
      >创建命名空间</el-button>
    </el-row>
    <el-table
      key="namespace"
      v-loading="loading"
      class="namespace-table table-fixed"
      :data="list"
      header-cell-class-name="listHeaderCell"
      :header-cell-style="{ background: '#F3F3F3', color: '#696969' }"
      border
    >
      <el-table-column
        prop="cloudVendor"
        label="云商名称"
        min-width="110"
        show-overflow-tooltip=""
      >
        <template slot-scope="scope">
          {{ cloudVendorToChn(scope.row.cloudVendor) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="k8sClusterName"
        label="集群名称"
        min-width="180"
        show-overflow-tooltip=""
      />
      <el-table-column
        prop="nameSpaceName"
        label="命名空间名称"
        min-width="200"
        show-overflow-tooltip=""
      />
      <el-table-column
        prop="describe"
        label="描述"
        min-width="190"
        show-overflow-tooltip=""
      />
      <el-table-column prop="deploymentNum" label="工作负载" min-width="150" />
      <el-table-column prop="podNum" label="容器组" min-width="150" />
      <el-table-column prop="svcNum" label="服务" min-width="150" />
      <el-table-column prop="cpuUse" label="CPU使用量" min-width="150" />
      <el-table-column prop="memUse" label="内存使用量" min-width="150" />
      <el-table-column prop="createTimeStr" label="创建时间" width="170" />
      <el-table-column label="操作" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            style="height: 22px; line-height: 22px; padding: 0 10px"
          >设置配额</el-button>
          <el-popover
            :ref="`popover-delete-${scope.$index}`"
            placement="top"
            width="300"
            style="margin-left: 10px"
          >
            <p>是否确认删除该命名空间？</p>
            <div style="text-align: right; margin: 0">
              <el-button
                size="mini"
                type="text"
                @click="
                  scope._self.$refs[`popover-delete-${scope.$index}`].doClose()
                "
              >取消</el-button>
              <el-button
                type="primary"
                size="mini"
                @click="deleteClick(scope)"
              >确定</el-button>
            </div>
            <el-button
              slot="reference"
              type="danger"
              style="height: 22px; line-height: 22px; padding: 0 10px"
              size="mini"
            >删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end" style="margin-top: 25px">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next"
        :page-sizes="[10, 15, 20]"
        :page-size="pageSize"
        :current-page.sync="pageNum"
        :total="totalNum"
        @current-change="changePage"
        @size-change="sizeChange"
      />
    </el-row>

    <el-dialog
      class="namespace-add-dialog"
      title="创建命名空间"
      :visible="showAddDialog"
      width="600px"
      top="15vh"
      :close-on-click-modal="false"
      @close="showAddDialog = false"
    >
      <el-form
        ref="addForm"
        size="medium"
        :rules="addRules"
        :model="addForm"
        label-width="auto"
      >
        <el-form-item label="名称" prop="nameSpaceName">
          <el-input
            v-model="addForm.nameSpaceName"
            placeholder="请输入命名空间名称"
          />
        </el-form-item>
        <el-form-item label="描述" prop="describe">
          <el-input
            v-model="addForm.describe"
            placeholder="请输入命名空间描述"
            rows="4"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="showAddDialog = false">取 消</el-button>
        <el-button
          type="primary"
          size="small"
          :loading="addConfirmLoading"
          @click="addConfirmClick"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getK8sClusterNameSpaces,
  addK8sClusterNameSpaces,
  delK8sClusterNameSpaces
} from '@/api/cnap/hybrid-cloud'
import commonTab from '../mixin/commonTab'

export default {
  name: 'NamespaceMgmt',
  mixins: [commonTab],
  data() {
    return {
      showAddDialog: false, // 显示添加弹窗
      // 校验规则
      addRules: {
        // 名称
        nameSpaceName: [
          { required: true, message: '请输入命名空间名称', trigger: 'blur' },
          {
            pattern: /^[a-z\d][a-z\d-]*$/,
            message: '请输入英文小写、数字或中划线的组合，且以英文和数字开头'
          }
        ],
        // 描述
        describe: [
          { required: true, message: '请输入命名空间描述', trigger: 'blur' }
        ]
      },
      // 表单
      addForm: {
        nameSpaceName: '', // 名称
        describe: '' // 描述
      },
      addConfirmLoading: false // 确定按钮loading
    }
  },
  methods: {
    /**
     * @description: 创建确定
     * @return {*}
     */
    addConfirmClick() {
      // 表单校验
      this.$refs.addForm.validate((valid, obj) => {
        if (valid) {
          this.addConfirmLoading = true
          // 请求接口
          addK8sClusterNameSpaces({
            ...this.clusterParams,
            ...this.addForm
          })
            .then((res) => {
              this.$message.success({
                message: '创建命名空间成功！',
                duration: '1500'
              })
              this.showAddDialog = false
              this.getNamespaceInfos() // 刷新数据
            })
            .finally(() => {
              this.addConfirmLoading = false
            })
        }
      })
    },
    /**
     * @description: 创建
     * @return {*}
     */
    addClick() {
      this.showAddDialog = true
      if (this.$refs.addForm) {
        this.$refs.addForm.resetFields() // 重置表单数据
      }
    },
    /**
     * @description: 删除
     * @param {*} scope
     * @return {*}
     */
    deleteClick(scope) {
      const row = scope.row
      // 请求接口
      delK8sClusterNameSpaces({
        ...this.clusterParams,
        nameSpaceName: row.nameSpaceName
      }).then((res) => {
        scope._self.$refs[`popover-delete-${scope.$index}`].doClose() // 关闭popover
        this.$message.success('删除成功')
        if (this.list.length === 1) {
          // 当前页只有一条数据时 删除完则请求上一页数据
          this.pageNum = Math.max(this.pageNum - 1, 1)
        }
        this.getNamespaceInfos() // 请求数据
      })
    },
    /**
     * @description: size变化
     * @param {*} size
     * @return {*}
     */
    sizeChange(size) {
      this.pageSize = size
      this.pageNum = 1
      this.getNamespaceInfos() // 请求数据
    },
    /**
     * @description: 页码改变
     * @param {*} pageNum
     * @return {*}
     */
    changePage(pageNum) {
      this.pageNum = pageNum
      this.getNamespaceInfos() // 请求数据
    },
    /**
     * @description: 清除关键字
     * @return {*}
     */
    clearClick() {
      this.searchKey = ''
      this.pageNum = 1
      this.getNamespaceInfos() // 请求数据
    },
    /**
     * @description: 查询
     * @return {*}
     */
    queryClick() {
      this.pageNum = 1
      this.getNamespaceInfos() // 请求数据
    },
    /**
     * @description: 获取命名空间列表
     * @return {*}
     */
    getNamespaceInfos() {
      // 请求
      getK8sClusterNameSpaces({
        ...this.clusterParams,
        keyword: this.searchKey,
        pageNo: this.pageNum,
        pageSize: this.pageSize
      })
        .then((res) => {
          if (res.data) {
            this.totalNum = res.data.totalNum
            this.list = Array.isArray(res.data.result) ? res.data.result : []
          } else {
            // 数据为空处理
            this.totalNum = 0
            this.list = []
          }
        })
        .finally(() => {
          this.loading = false // 取消loading
        })
    },
    /**
     * @description: 初始化
     * @return {*}
     */
    initTabData() {
      this.loading = true
      this.pageNum = 1
      this.getNamespaceInfos() // 请求数据
    }
  }
}
</script>

<style lang="less" scoped>
.namespace-mgmt-ctn {
  .namespace-table {
    /deep/ .el-table::before {
      background-color: #e9e9e9;
    }
    /deep/ .listHeaderCell {
      height: 40px;
      padding: 0;
      box-sizing: border-box;
      &:first-child {
        padding-left: 20px;
      }
    }
    /deep/ .el-table__body {
      .el-table__row td {
        height: 40px;
        padding: 0;
        &:first-child {
          padding-left: 20px;
        }
      }
    }
  }
}
</style>
